package cn.wyz.org.controller;

import cn.wyz.basic.util.AjaxResult;
import cn.wyz.basic.util.PlusQueryList;
import cn.wyz.org.domain.DictionaryDetail;
import cn.wyz.org.query.DictionaryDetailQuery;
import cn.wyz.org.service.IDictionaryDetailService;
import cn.wyz.org.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailsController {
    @Autowired
    private IDictionaryDetailService detailService;
    @Autowired
    private ISystemDictionaryTypeService typeService;

    //查询全部记录  GET
    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return detailService.selectAll();
    }

    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return detailService.selectById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            detailService.deleteById(id);
            return AjaxResult.make();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make()
                    .setSuccess(false)
                    .setMessage("烂中烂,直接开摆!");
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail detail){
        try {
            if (detail.getId() == null){
                detail.setTypes_id(detail.getType().getId());
                detailService.insert(detail);
            } else {
                detailService.update(detail);
            }
            return AjaxResult.make();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make()
                    .setSuccess(false)
                    .setMessage("烂中烂,直接开摆!");
        }
    }

    @PostMapping
    public PlusQueryList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query){
        return detailService.queryData(query);
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> idList){
        try {
            detailService.patchDelete(idList);
            return AjaxResult.make();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make()
                    .setSuccess(false)
                    .setMessage("烂中烂,直接开摆!");
        }
    }

    @PostMapping("/more")
    public PlusQueryList<DictionaryDetail> queryDetails(@RequestBody DictionaryDetailQuery query){
        return detailService.queryDetails(query);
    }


}
